package com.singi.controller;

import cn.binarywang.wx.miniapp.api.WxMaQrcodeService;
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.eova.common.base.BaseController;
import com.eova.common.utils.util.StringUtils;
import com.jfinal.aop.Clear;
import com.jfinal.core.paragetter.Para;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.singi.config.WxMaConfiguration;
import com.singi.config.WxMpConfiguration;
import me.chanjar.weixin.common.bean.WxJsapiSignature;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2020-2-23.
 */

@Clear
public class StoreController extends BaseController {

    private String domain_url="https://www.xinjisx.com";
    //获取轮播图
    public void getAdvantage(){
        List<Record> advantageTypedata =  Db.find("select * from xj_advantage;");
        renderJson(advantageTypedata);
    }

    //切换不同市场获取商家列表信息
    public void getMarketByType(@Para("type_id") String type_id,@Para("language") String language,@Para("page") int page){
        int page_num = page - 1;
        if(language.equalsIgnoreCase("cn")){
            List<Record> marketTypedata =  Db.find("SELECT DISTINCT\n" +
                    "\ta.id,\n" +
                    "\ta.category,\n" +
                    "\ta.mainlabel,\n" +
                    "\ta.store_name as store_name,\n" +
                    "\ta.store_no,\n" +
                    "\ta.logo,\n" +
                    "\ta.phone,\n" +
                    "  a.product_show_pic\n" +
                    "FROM\n" +
                    "\txj_store a\n" +
                    "where a.is_hot = 1 " +
                    "and a.market_type = ? " +
                    "order by id asc " +
                    "limit ?,5",type_id,page_num);

            for(int i=0;i<marketTypedata.size();i++){

                //解析产品图URL
                if(!StringUtils.isEmpty(marketTypedata.get(i).get("product_show_pic"))){
                    String[] productList = marketTypedata.get(i).get("product_show_pic").toString().split(",");
                    JSONArray productArray = new JSONArray();
                    for(int j=0;j<productList.length; j++){
                        JSONObject productOject = new JSONObject();
                        String product_str = domain_url+"/store/product/"+productList[j];
                        productOject.put("url",product_str);
                        productArray.add(productOject);
                    }
                    marketTypedata.get(i).set("product_show_pic", productArray);
                }

            }

            for(int i = 0; i<marketTypedata.size();i++){
                marketTypedata.get(i).set("logo",domain_url+"/store/logo/"+marketTypedata.get(i).get("logo"));
            }
            renderJson(marketTypedata);
        }else{
            List<Record> marketTypedata =  Db.find("SELECT\n" +
                    "\ta.id,\n" +
                    "\ta.category,\n" +
                    "\ta.mainlabel_en as mainlabel,\n" +
                    "\ta.store_name_en as store_name,\n" +
                    "\ta.store_no,\n" +
                    "\ta.logo,\n" +
                    "\ta.phone,\n" +
                    "  a.product_show_pic\n" +
                    "FROM\n" +
                    "\txj_store a\n" +
                    "where a.is_hot = 1 and a.market_type = ?",type_id);

            for(int i=0;i<marketTypedata.size();i++){


                //产品图
                if(!StringUtils.isEmpty(marketTypedata.get(i).get("product_show_pic"))) {
                    String[] productList = marketTypedata.get(i).get("product_show_pic").toString().split(",");
                    JSONArray productArray = new JSONArray();
                    for (int j = 0; j < productList.length; j++) {
                        JSONObject productOject = new JSONObject();
                        String product_str = domain_url + "/store/product/" + productList[j];
                        productOject.put("url", product_str);
                        productArray.add(productOject);
                    }
                    marketTypedata.get(i).set("product_show_pic", productArray);
                }
            }

            for(int i = 0; i<marketTypedata.size();i++){
                marketTypedata.get(i).set("logo",domain_url+"/store/logo/"+marketTypedata.get(i).get("logo"));
            }


            renderJson(marketTypedata);
        }

    }

    public void getMarketByTypeAll(@Para("type_id") String type_id,@Para("language") String language,@Para("page") int page){
        int page_num = page - 1;
        if(language.equalsIgnoreCase("cn")){
            List<Record> marketTypedata =  Db.find("SELECT\n" +
                    "\ta.id,\n" +
                    "\ta.category,\n" +
                    "\ta.market_type,\n" +
                    "\ta.mainlabel,\n" +
                    "\ta.store_name as store_name,\n" +
                    "\ta.store_no,\n" +
                    "\ta.logo,\n" +
                    "\ta.phone,\n" +
                    "  a.product_show_pic\n" +
                    "FROM\n" +
                    "\txj_store a\n" +
                    "where  a.market_type = ? " +
                    "order by is_up desc,id asc " +
                    "limit ?,10",type_id,page_num);

            for(int i=0;i<marketTypedata.size();i++){

                //解析产品图URL
                if(!StringUtils.isEmpty(marketTypedata.get(i).get("product_show_pic"))){
                    String[] productList = marketTypedata.get(i).get("product_show_pic").toString().split(",");
                    JSONArray productArray = new JSONArray();
                    for(int j=0;j<productList.length; j++){
                        JSONObject productOject = new JSONObject();
                        String product_str = domain_url+"/store/product/"+productList[j];
                        productOject.put("url",product_str);
                        productArray.add(productOject);
                    }
                    marketTypedata.get(i).set("product_show_pic", productArray);
                }

                if(language.equalsIgnoreCase("cn")){
                    if(marketTypedata.get(i).get("market_type").toString().equalsIgnoreCase("1")){
                        marketTypedata.get(i).set("market_type","信基沙溪(广州)");
                    }else if(marketTypedata.get(i).get("market_type").toString().equalsIgnoreCase("2")){
                        marketTypedata.get(i).set("market_type","信基豪泰(广州)");
                    }else if(marketTypedata.get(i).get("market_type").toString().equalsIgnoreCase("3")){
                        marketTypedata.get(i).set("market_type","信基沙溪(沈阳)");
                    }
                }else{
                    if(marketTypedata.get(i).get("market_type").toString().equalsIgnoreCase("1")){
                        marketTypedata.get(i).set("market_type","Shaxi(Guangzhou)");
                    }else if(marketTypedata.get(i).get("market_type").toString().equalsIgnoreCase("2")){
                        marketTypedata.get(i).set("market_type","Haotai(Guangzhou)");
                    }else if(marketTypedata.get(i).get("market_type").toString().equalsIgnoreCase("3")){
                        marketTypedata.get(i).set("market_type","Shaxi(Shenyang)");
                    }
                }

            }

            for(int i = 0; i<marketTypedata.size();i++){
                marketTypedata.get(i).set("logo",domain_url+"/store/logo/"+marketTypedata.get(i).get("logo"));
            }
            renderJson(marketTypedata);
        }else{
            List<Record> marketTypedata =  Db.find("SELECT\n" +
                    "\ta.id,\n" +
                    "\ta.category,\n" +
                    "\ta.market_type,\n" +
                    "\ta.mainlabel_en as mainlabel,\n" +
                    "\ta.store_name_en as store_name,\n" +
                    "\ta.store_no,\n" +
                    "\ta.logo,\n" +
                    "\ta.phone,\n" +
                    "  a.product_show_pic\n" +
                    "FROM\n" +
                    "\txj_store a\n" +
                    "where a.is_hot = 1 and a.market_type = ?",type_id);

            for(int i=0;i<marketTypedata.size();i++){

                //产品图
                if(!StringUtils.isEmpty(marketTypedata.get(i).get("product_show_pic"))) {
                    String[] productList = marketTypedata.get(i).get("product_show_pic").toString().split(",");
                    JSONArray productArray = new JSONArray();
                    for (int j = 0; j < productList.length; j++) {
                        JSONObject productOject = new JSONObject();
                        String product_str = domain_url + "/store/product/" + productList[j];
                        productOject.put("url", product_str);
                        productArray.add(productOject);
                    }
                    marketTypedata.get(i).set("product_show_pic", productArray);
                }

                if(language.equalsIgnoreCase("cn")){
                    if(marketTypedata.get(i).get("market_type").toString().equalsIgnoreCase("1")){
                        marketTypedata.get(i).set("market_type","信基沙溪(广州)");
                    }else if(marketTypedata.get(i).get("market_type").toString().equalsIgnoreCase("2")){
                        marketTypedata.get(i).set("market_type","信基豪泰(广州)");
                    }else if(marketTypedata.get(i).get("market_type").toString().equalsIgnoreCase("3")){
                        marketTypedata.get(i).set("market_type","信基沙溪(沈阳)");
                    }
                }else{
                    if(marketTypedata.get(i).get("market_type").toString().equalsIgnoreCase("1")){
                        marketTypedata.get(i).set("market_type","Shaxi(Guangzhou)");
                    }else if(marketTypedata.get(i).get("market_type").toString().equalsIgnoreCase("2")){
                        marketTypedata.get(i).set("market_type","Haotai(Guangzhou)");
                    }else if(marketTypedata.get(i).get("market_type").toString().equalsIgnoreCase("3")){
                        marketTypedata.get(i).set("market_type","Shaxi(Shenyang)");
                    }
                }
            }

            for(int i = 0; i<marketTypedata.size();i++){
                marketTypedata.get(i).set("logo",domain_url+"/store/logo/"+marketTypedata.get(i).get("logo"));
            }


            renderJson(marketTypedata);
        }

    }

    public void getAreaPic(@Para("area") String area){
        int area_id = Integer.parseInt(area);
        String area_url = "";
        switch (area_id){
                case 24:
                    area_url="/area/A.jpg";
                    break;
                case 25:
                    area_url="/area/B.jpg";
                    break;
                case 26:
                    area_url="/area/C.jpg";
                    break;
                case 27:
                    area_url="/area/D.jpg";
                    break;
                case 28:
                    area_url="/area/E.jpg";
                    break;
                case 29:
                    area_url="/area/F.jpg";
                    break;
                case 30:
                    area_url="/area/G.jpg";
                    break;
                case 31:
                    area_url="/area/H.jpg";
                    break;
                case 32:
                    area_url="/area/I.jpg";
                    break;
                case 37:
                    area_url="/area/haotai.jpg";
                    break;
                case 38:
                    area_url="/area/haotai.jpg";
                    break;
                case 39:
                    area_url="/area/haotai.jpg";
                    break;
                case 40:
                    area_url="/area/haotai.jpg";
                    break;
                case 41:
                    area_url="/area/haotai.jpg";
                    break;
                case 42:
                    area_url="/area/haotai.jpg";
                    break;
                case 43:
                    area_url="/area/haotai.jpg";
                    break;
                case 44:
                    area_url="/area/haotai.jpg";
                    break;
                case 45:
                    area_url="/area/haotai.jpg";
                    break;
                case 46:
                    area_url="/area/haotai.jpg";
                    break;
                case 47:
                    area_url="/area/haotai.jpg";
                    break;
                case 48:
                    area_url="/area/haotai.jpg";
                    break;
                case 49:
                    area_url="/area/haotai.jpg";
                    break;
                case 50:
                    area_url="/area/haotai.jpg";
                    break;
                case 33:
                    area_url="/area/sy1.jpg";
                    break;
                case 34:
                    area_url="/area/sy2.jpg";
                    break;
                case 35:
                    area_url="/area/sy3.jpg";
                    break;
                case 36:
                    area_url="/area/sy4.jpg";
                    break;

            }

           area_url = domain_url+area_url;
           renderJson(area_url);


    }

    public void getMarketByArea(@Para("area") String area,@Para("language") String language,@Para("page") int page){

        if(language.equalsIgnoreCase("cn")){
            List<Record>  marketTypedata =  Db.find("SELECT\n" +
                    "\ta.id,\n" +
                    "\ta.market_type,\n" +
                    "\ta.category,\n" +
                    "\ta.mainlabel,\n" +
                    "\ta.store_name,\n" +
                    "\ta.store_no,\n" +
                    "\ta.belong_area,\n" +
                    "\ta.logo,\n" +
                    "\ta.phone,\n" +
                    "  a.product_show_pic\n" +
                    "FROM\n" +
                    "\txj_store a\n" +
                    "where a.belong_area = ?" +
                    "order by id asc \n" +
                    "limit ?,10",area,page);

            for(int i = 0; i<marketTypedata.size();i++){
                if(!StringUtils.isEmpty(marketTypedata.get(i).get("logo"))){
                    marketTypedata.get(i).set("logo",domain_url+"/store/logo/"+marketTypedata.get(i).get("logo"));
                }

                if(!StringUtils.isEmpty(marketTypedata.get(i).get("product_show_pic"))){
                    //产品图
                    String[] productList = marketTypedata.get(i).get("product_show_pic").toString().split(",");
                    JSONArray productArray = new JSONArray();
                    for(int j=0;j<productList.length; j++){
                        JSONObject productOject = new JSONObject();
                        String product_str = domain_url+"/store/product/"+productList[j];
                        productOject.put("url",product_str);
                        productArray.add(productOject);
                    }
                    marketTypedata.get(i).set("product_show_pic", productArray);
                }

                if(language.equalsIgnoreCase("cn")){
                    if(marketTypedata.get(i).get("market_type").toString().equalsIgnoreCase("1")){
                        marketTypedata.get(i).set("market_type","信基沙溪(广州)");
                    }else if(marketTypedata.get(i).get("market_type").toString().equalsIgnoreCase("2")){
                        marketTypedata.get(i).set("market_type","信基豪泰(广州)");
                    }else if(marketTypedata.get(i).get("market_type").toString().equalsIgnoreCase("3")){
                        marketTypedata.get(i).set("market_type","信基沙溪(沈阳)");
                    }
                }else{
                    if(marketTypedata.get(i).get("market_type").toString().equalsIgnoreCase("1")){
                        marketTypedata.get(i).set("market_type","Shaxi(Guangzhou)");
                    }else if(marketTypedata.get(i).get("market_type").toString().equalsIgnoreCase("2")){
                        marketTypedata.get(i).set("market_type","Haotai(Guangzhou)");
                    }else if(marketTypedata.get(i).get("market_type").toString().equalsIgnoreCase("3")){
                        marketTypedata.get(i).set("market_type","Shaxi(Shenyang)");
                    }
                }
            }
            renderJson(marketTypedata);
        }else{
            List<Record> marketTypedata =  Db.find("SELECT\n" +
                    "\ta.id,\n" +
                    "\ta.category,\n" +
                    "\ta.market_type,\n" +
                    "\ta.mainlabel_en as mainlabel,\n" +
                    "\ta.store_name_en as store_name,\n" +
                    "\ta.store_no,\n" +
                    "\ta.logo,\n" +
                    "\ta.phone,\n" +
                    "  a.product_show_pic\n" +
                    "FROM\n" +
                    "\txj_store a\n" +
                    "where a.belong_area = ?" +
                    "order by id asc \n" +
                    "limit ?,10",area,page);


            for(int i = 0; i<marketTypedata.size();i++){
                if(!StringUtils.isEmpty(marketTypedata.get(i).get("logo"))){
                    marketTypedata.get(i).set("logo",domain_url+"/store/logo/"+marketTypedata.get(i).get("logo"));
                }
                if(!StringUtils.isEmpty(marketTypedata.get(i).get("product_show_pic"))){
                    //产品图
                    String[] productList = marketTypedata.get(i).get("product_show_pic").toString().split(",");
                    JSONArray productArray = new JSONArray();
                    for(int j=0;j<productList.length; j++){
                        JSONObject productOject = new JSONObject();
                        String product_str = domain_url+"/store/product/"+productList[j];
                        productOject.put("url",product_str);
                        productArray.add(productOject);
                    }
                    marketTypedata.get(i).set("product_show_pic", productArray);
                }

                if(language.equalsIgnoreCase("cn")){
                    if(marketTypedata.get(i).get("market_type").toString().equalsIgnoreCase("1")){
                        marketTypedata.get(i).set("market_type","信基沙溪(广州)");
                    }else if(marketTypedata.get(i).get("market_type").toString().equalsIgnoreCase("2")){
                        marketTypedata.get(i).set("market_type","信基豪泰(广州)");
                    }else if(marketTypedata.get(i).get("market_type").toString().equalsIgnoreCase("3")){
                        marketTypedata.get(i).set("market_type","信基沙溪(沈阳)");
                    }
                }else{
                    if(marketTypedata.get(i).get("market_type").toString().equalsIgnoreCase("1")){
                        marketTypedata.get(i).set("market_type","Shaxi(Guangzhou)");
                    }else if(marketTypedata.get(i).get("market_type").toString().equalsIgnoreCase("2")){
                        marketTypedata.get(i).set("market_type","Haotai(Guangzhou)");
                    }else if(marketTypedata.get(i).get("market_type").toString().equalsIgnoreCase("3")){
                        marketTypedata.get(i).set("market_type","Shaxi(Shenyang)");
                    }
                }

            }
            renderJson(marketTypedata);
        }




    }

    /*
    * 获取所有市场品类商家
     */
    public void getAllStoreByType(@Para("type") String type,@Para("language") String language,@Para("page") int page){
        List<Record> storeList;
        int page_num = page-1;
        if(language.equalsIgnoreCase("cn")){
            String query_sql = "SELECT\n" +
                    "\tid,\n" +
                    "\tmarket_type,\n" +
                    "\tcategory,\n" +
                    "\tmainlabel,\n" +
                    "\tstore_name,\n" +
                    "\tstore_no,\n" +
                    "\tlogo,\n" +
                    "\tphone,\n" +
                    "\tproduct_show_pic\n" +
                    "FROM\n" +
                    "\txj_store\n";
        if(type.equalsIgnoreCase("999")){
            query_sql +=  "\torder by is_up desc,id asc \n"+
                    "\tlimit ?,8\n";
        }else{
            query_sql += "WHERE\n";

            if(type.equalsIgnoreCase("6") ){
                query_sql  +="\tcategory LIKE '%"+type+"%' and category not LIKE '%16%'\n" +

                        "order by is_up desc,id asc " +
                        "limit ?,10";;

            }else if(type.equalsIgnoreCase("7")){
                query_sql +="\tcategory LIKE '%"+type+"%' and category not LIKE '%17%'\n" +

                        "order by is_up desc,id asc " +
                        "limit ?,10";
            }else{
                query_sql += "\tcategory LIKE '%"+type+"%'\n" +

                        "order by is_up desc,id asc " +
                        "limit ?,10";
            }
//                    "\tcategory LIKE '%"+type+"%'\n"+
//                    "\torder by is_up desc,id asc \n"+
//                    "\tlimit "+page_num+",8\n";
        }

            storeList = Db.find(query_sql,page_num);



        }else{
            String query_sql = "SELECT\n" +
                    "\tid,\n" +
                    "\tmarket_type,\n" +
                    "\tcategory,\n" +
                    "\tmainlabel_en as mainlabel,\n" +
                    "\tstore_name_en as store_name,\n" +
                    "\tstore_no,\n" +
                    "\tlogo,\n" +
                    "\tphone,\n" +
                    "\tproduct_show_pic\n" +
                    "FROM\n" +
                    "\txj_store\n";

            if(type.equalsIgnoreCase("999")){
                query_sql +=  "\torder by is_up desc,id asc \n"+
                        "\tlimit ?,8\n";
            }else{
                query_sql += "WHERE\n";

                if(type.equalsIgnoreCase("6") ){
                    query_sql  +="\tcategory LIKE '%"+type+"%' and category not LIKE '%16%'\n" +

                            "order by is_up desc,id asc " +
                            "limit ?,10";;

                }else if(type.equalsIgnoreCase("7")){
                    query_sql +="\tcategory LIKE '%"+type+"%' and category not LIKE '%17%'\n" +

                            "order by is_up desc,id asc " +
                            "limit ?,10";
                }else{
                    query_sql += "\tcategory LIKE '%"+type+"%'\n" +

                            "order by is_up desc,id asc " +
                            "limit ?,10";
                }
            }

            storeList = Db.find(query_sql,page_num);


        }

        for(int i = 0; i<storeList.size();i++){
            if(!StringUtils.isEmpty(storeList.get(i).get("logo"))){
                storeList.get(i).set("logo",domain_url+"/store/logo/"+storeList.get(i).get("logo"));
            }
            if(language.equalsIgnoreCase("cn")){
                if(storeList.get(i).get("market_type").toString().equalsIgnoreCase("1")){
                    storeList.get(i).set("market_type","信基沙溪(广州)");
                }else if(storeList.get(i).get("market_type").toString().equalsIgnoreCase("2")){
                    storeList.get(i).set("market_type","信基豪泰(广州)");
                }else if(storeList.get(i).get("market_type").toString().equalsIgnoreCase("3")){
                    storeList.get(i).set("market_type","信基沙溪(沈阳)");
                }
            }else{
                if(storeList.get(i).get("market_type").toString().equalsIgnoreCase("1")){
                    storeList.get(i).set("market_type","Shaxi(Guangzhou)");
                }else if(storeList.get(i).get("market_type").toString().equalsIgnoreCase("2")){
                    storeList.get(i).set("market_type","Haotai(Guangzhou)");
                }else if(storeList.get(i).get("market_type").toString().equalsIgnoreCase("3")){
                    storeList.get(i).set("market_type","Shaxi(Shenyang)");
                }
            }



            if(!StringUtils.isEmpty(storeList.get(i).get("product_show_pic"))){
                //产品图
                String[] productList = storeList.get(i).get("product_show_pic").toString().split(",");
                JSONArray productArray = new JSONArray();
                for(int j=0;j<productList.length; j++){
                    JSONObject productOject = new JSONObject();
                    String product_str = domain_url+"/store/product/"+productList[j];
                    productOject.put("url",product_str);
                    productArray.add(productOject);
                }
                storeList.get(i).set("product_show_pic", productArray);
            }


        }

        renderJson(storeList);
    }

    /*
    *品牌导购页面点击主营跳转到列表页接口
    */
    public void getStoreByType(@Para("type") String type,@Para("market_type") int market_type,@Para("language") String language,@Para("page") int page){
        List<Record> storeList;
        int page_num = page - 1;
        if(language.equalsIgnoreCase("cn")){
            String query_sql = "SELECT\n" +
                    "\tid,\n" +
                    "\tmarket_type,\n" +
                    "\tcategory,\n" +
                    "\tmainlabel,\n" +
                    "\tstore_name,\n" +
                    "\tstore_no,\n" +
                    "\tlogo,\n" +
                    "\tphone,\n" +
                    "\tproduct_show_pic\n" +
                    "FROM\n" +
                    "\txj_store\n" +
                    "WHERE\n";
//                    "\tcategory LIKE '%"+type+"%'\n" +
//                    "AND market_type = ?";
            if(type.equalsIgnoreCase("6") ){
                query_sql  +="\tcategory LIKE '%"+type+"%' and category not LIKE '%16%'\n" +
                        "AND market_type = ? "+
                        "order by is_up desc,id asc " +
                        "limit ?,10";;

            }else if(type.equalsIgnoreCase("7")){
                query_sql +="\tcategory LIKE '%"+type+"%' and category not LIKE '%17%'\n" +
                        "AND market_type = ? " +
                        "order by is_up desc,id asc " +
                        "limit ?,10";
            }else{
                query_sql += "\tcategory LIKE '%"+type+"%'\n" +
                    "AND market_type = ? "+
                        "order by is_up desc,id asc " +
                        "limit ?,10";
            }

            System.err.println();

            storeList =  Db.find(query_sql,market_type,page_num);
//            storeList =  Db.find("SELECT\n" +
//                    "\tid,\n" +
//                    "\tmarket_type,\n" +
//                    "\tcategory,\n" +
//                    "\tmainlabel,\n" +
//                    "\tstore_name,\n" +
//                    "\tstore_no,\n" +
//                    "\tlogo,\n" +
//                    "\tphone,\n" +
//                    "\tproduct_show_pic\n" +
//                    "FROM\n" +
//                    "\txj_store\n" +
//                    "WHERE\n" +
//                    "\tcategory LIKE '%"+type+"%'\n" +
//                    "AND market_type = ?",market_type);
//            for(int i = 0; i<storeList.size();i++){
//                //主营
//                String[] categoryList = storeList.get(i).get("category").toString().split(",");
//                String categoryArray = "";
//                for(int j=0;j<categoryList.length; j++){
//                    String category_str = Db.queryStr("select enum_value from xj_enum_item where id = ?",categoryList[j]);
//                    categoryArray += category_str+",";
//                    if(j == categoryList.length-1){
//                        categoryArray = categoryArray.substring(0,categoryArray.length()-1);
//                    }
//                }
//                storeList.get(i).set("category",categoryArray);
//            }

        }else{
            storeList =  Db.find("SELECT\n" +
                    "\tid,\n" +
                    "\tmarket_type,\n" +
                    "\tcategory,\n" +
                    "\tmainlabel_en as mainlabel,\n" +
                    "\tstore_name_en as store_name,\n" +
                    "\tstore_no,\n" +
                    "\tlogo,\n" +
                    "\tphone,\n" +
                    "\tproduct_show_pic\n" +
                    "FROM\n" +
                    "\txj_store\n" +
                    "WHERE\n" +
                    "\tcategory LIKE '%"+type+"%'\n" +
                    "AND market_type = ?",market_type);

//            for(int i = 0; i<storeList.size();i++){
//                //主营
//                String[] categoryList = storeList.get(i).get("category").toString().split(",");
//                //JSONObject categoryArray = new JSONObject();
//                String categoryArray = "";
//                for(int j=0;j<categoryList.length; j++){
//                    String category_str = Db.queryStr("select vcode from xj_enum_item where id = ?",categoryList[j]);
//                    categoryArray += category_str+",";
//                    if(j == categoryList.length-1){
//                        categoryArray = categoryArray.substring(0,categoryArray.length()-1);
//                    }
//                }
//                storeList.get(i).set("category",categoryArray);
//            }
        }



        for(int i = 0; i<storeList.size();i++){
            if(!StringUtils.isEmpty(storeList.get(i).get("logo"))){
                storeList.get(i).set("logo",domain_url+"/store/logo/"+storeList.get(i).get("logo"));
            }

            if(language.equalsIgnoreCase("cn")){
                if(storeList.get(i).get("market_type").toString().equalsIgnoreCase("1")){
                    storeList.get(i).set("market_type","信基沙溪(广州)");
                }else if(storeList.get(i).get("market_type").toString().equalsIgnoreCase("2")){
                    storeList.get(i).set("market_type","信基豪泰(广州)");
                }else if(storeList.get(i).get("market_type").toString().equalsIgnoreCase("3")){
                    storeList.get(i).set("market_type","信基沙溪(沈阳)");
                }
            }else{
                if(storeList.get(i).get("market_type").toString().equalsIgnoreCase("1")){
                    storeList.get(i).set("market_type","Shaxi(Guangzhou)");
                }else if(storeList.get(i).get("market_type").toString().equalsIgnoreCase("2")){
                    storeList.get(i).set("market_type","Haotai(Guangzhou)");
                }else if(storeList.get(i).get("market_type").toString().equalsIgnoreCase("3")){
                    storeList.get(i).set("market_type","Shaxi(Shenyang)");
                }
            }

            if(!StringUtils.isEmpty(storeList.get(i).get("product_show_pic"))){
                //产品图
                String[] productList = storeList.get(i).get("product_show_pic").toString().split(",");
                JSONArray productArray = new JSONArray();
                for(int j=0;j<productList.length; j++){
                    JSONObject productOject = new JSONObject();
                    String product_str = domain_url+"/store/product/"+productList[j];
                    productOject.put("url",product_str);
                    productArray.add(productOject);
                }
                storeList.get(i).set("product_show_pic", productArray);
            }



        }

        renderJson(storeList);
    }

    public void getStoreDetail(@Para("store_id") String store_id){
        Record storeDetail = Db.findFirst("SELECT\n" +
                "\tid,\n" +
                "\tmarket_type,\n" +
                "\tcategory,\n" +
                "\tmainlabel,\n" +
                "\tmainlabel_en,\n" +
                "\tstore_name,\n" +
                "\tstore_name_en,\n" +
                "\tstore_no,\n" +
                "\tmain_pic,\n" +
                "\tphone,\n" +
                "\tlogo,\n" +
                "\tproduct_show_pic,\n" +
                "\tIFNULL(email, '') email,\n" +
                "\tIFNULL(website, '') website,\n" +
                "\tIFNULL(address, '') address,\n" +
                "\tIFNULL(address_en, '') address_en,\n" +
                "\tIFNULL(stores_live_img, '') address_en,\n" +
                "\tproduct_show_pic,\n" +
                "\tlongitude,\n" +
                "\tlatitude,\n" +
                "\tstores_live,\n" +
                "\tstores_live_img\n" +
                "FROM\n" +
                "\txj_store\n" +
                "where id = ?;",store_id);

        if(storeDetail.get("market_type").toString().equalsIgnoreCase("1")){
            if(StringUtils.isEmpty(storeDetail.get("longitude"))){
                storeDetail.set("longitude","113.3356846400");
            }
            if(StringUtils.isEmpty(storeDetail.get("latitude"))){
                storeDetail.set("latitude","23.0354510600");
            }
        }else if(storeDetail.get("market_type").toString().equalsIgnoreCase("2")){
            if(StringUtils.isEmpty(storeDetail.get("longitude"))){
                storeDetail.set("longitude","113.3177230000");
            }
            if(StringUtils.isEmpty(storeDetail.get("latitude"))){
                storeDetail.set("latitude","23.0365580000");
            }
        }else if(storeDetail.get("market_type").toString().equalsIgnoreCase("3")){
            if(StringUtils.isEmpty(storeDetail.get("longitude"))){
                storeDetail.set("longitude","123.3961671300");
            }
            if(StringUtils.isEmpty(storeDetail.get("latitude"))){
                storeDetail.set("latitude","41.9492745600");
            }
        }

        storeDetail.set("stores_live_img",domain_url+"/store/vr/"+storeDetail.get("stores_live_img"));

        //商店logo
        storeDetail.set("logo",domain_url+"/store/logo/"+storeDetail.get("logo"));

        storeDetail.set("main_pic",domain_url+"/store/top/"+storeDetail.get("main_pic"));

        //产品图
        if(!StringUtils.isEmpty(storeDetail.get("product_show_pic"))){
            String[] productList = storeDetail.get("product_show_pic").toString().split(",");
            JSONArray productArray = new JSONArray();
            for(int j=0;j<productList.length; j++){
                JSONObject productOject = new JSONObject();
                String product_str = domain_url+"/store/product/"+productList[j];
                productOject.put("url",product_str);
                productArray.add(productOject);
            }
            storeDetail.set("product_show_pic", productArray);
        }



        String[] categoryList = storeDetail.get("mainlabel").toString().split(",");
        JSONArray categoryArray = new JSONArray();
        for(int j=0;j<categoryList.length; j++){
            JSONObject categoryOject = new JSONObject();
            //String category_str = Db.queryStr("select enum_value from xj_enum_item where id = ?",categoryList[j]);
            categoryOject.put("name",categoryList[j]);
            categoryArray.add(categoryOject);
        }
        storeDetail.set("category", categoryArray);

        String[] categoryList_en = storeDetail.get("mainlabel_en").toString().split(",");
        JSONArray categoryArray_en = new JSONArray();
        for(int j=0;j<categoryList_en.length; j++){
            JSONObject categoryOject_en = new JSONObject();
            //String category_str = Db.queryStr("select vcode from xj_enum_item where id = ?",categoryList[j]);
            categoryOject_en.put("name",categoryList_en[j]);
            categoryArray_en.add(categoryOject_en);
        }
        storeDetail.set("market_type",categoryArray_en);

        renderJson(storeDetail);
    }

    /*
    *获取店铺品牌介绍内容
     */
    public void getStoreIntroduce(@Para("store_id") String store_id,@Para("language") String language){
        if(language.equalsIgnoreCase("cn")){
            Record storeIntroduce = Db.findFirst("SELECT\n" +
                    "\tid,\n" +
                    "\tbelong_area,\n" +
                    "\tmarket_type,\n" +
                    "\tstore_name,\n" +
                    "\tbrandIntroduction\n" +
                    "FROM\n" +
                    "\txj_store\n" +
                    "where id = ?;",store_id);

            int belong_area = Integer.parseInt(storeIntroduce.get("belong_area"));
            int market_type = storeIntroduce.get("market_type");

            String store_area = Db.queryStr("select enum_value from xj_enum_item where id = ?",belong_area);
            String store_type = Db.queryStr("select enum_value from xj_enum_item where id = ?",market_type);

            storeIntroduce.set("market_type",store_type+"("+store_area+")");
            renderJson(storeIntroduce);
        }else{
            Record storeIntroduce = Db.findFirst("SELECT\n" +
                    "\tid,\n" +
                    "\tbelong_area,\n" +
                    "\tmarket_type,\n" +
                    "\tstore_name_en as store_name,\n" +
                    "\tbrandIntroduction_en as brandIntroduction\n" +
                    "FROM\n" +
                    "\txj_store\n" +
                    "where id = ?;",store_id);

            int belong_area = Integer.parseInt(storeIntroduce.get("belong_area"));
            int market_type = storeIntroduce.get("market_type");

            String store_area = Db.queryStr("select vcode from xj_enum_item where id = ?",belong_area);
            String store_type = Db.queryStr("select enum_value from xj_enum_item where id = ?",market_type);
            if(store_type.equalsIgnoreCase("沙溪(广州)")){
                store_type = "Xinji Shaxi(Guangzhou)";
            }else if(store_type.equalsIgnoreCase("豪泰(广州)")){
                store_type = "Xinji Hotelex(Guangzhou)";
            }else if(store_type.equalsIgnoreCase("沙溪(沈阳)")){
                store_type = "Xinji Shaxi(Shenyang)";
            }
            storeIntroduce.set("market_type",store_type+"("+store_area+")");
            renderJson(storeIntroduce);
        }


    }


    public void getPhoto(@Para("store_id") String store_id){
         String  photoList = Db.queryStr("select product_show_pic from xj_store where id = ?",store_id);
        String[] productList = photoList.split(",");
        JSONArray productArray = new JSONArray();
        for(int j=0;j<productList.length; j++){
            JSONObject productOject = new JSONObject();
            String product_str = domain_url+"/store/product/"+productList[j];
            productOject.put("url",product_str);
            productArray.add(productOject);
        }
        renderJson(productArray);
    }

    //搜索查询
    public void getStoreBySearch(@Para("search_str") String search_str,@Para("language") String language,@Para("page") int page){
        int page_num = page - 1;
        List<Record> storeList;
        if(language.equalsIgnoreCase("cn")){
            storeList =  Db.find("SELECT\n" +
                    "\tid,\n" +
                    "\tmarket_type,\n" +
                    "\tcategory,\n" +
                    "\tmainlabel,\n" +
                    "\tstore_name,\n" +
                    "\tstore_no,\n" +
                    "\tlogo,\n" +
                    "\tphone,\n" +
                    "\tproduct_show_pic\n" +
                    "FROM\n" +
                    "\txj_store\n" +
                    "WHERE\n" +
                    "\tstore_name LIKE '%"+search_str+"%' \n" +
                    "\torder by is_up desc,id asc \n" +
                    "\tlimit ?,10",page_num);

        }else{
            storeList =  Db.find("SELECT\n" +
                    "\tid,\n" +
                    "\tmarket_type,\n" +
                    "\tcategory,\n" +
                    "\tmainlabel_en as mainlabel,\n" +
                    "\tstore_name_en as store_name,\n" +
                    "\tstore_no,\n" +
                    "\tlogo,\n" +
                    "\tproduct_show_pic\n" +
                    "FROM\n" +
                    "\txj_store\n" +
                    "WHERE\n" +
                    "\tstore_name_en LIKE '%"+search_str+"%' \n"+
                    "\torder by is_up desc,id asc \n" +
                    "\tlimit ?,10",page_num);

//            for(int i = 0; i<storeList.size();i++){
//                //主营
//                String[] categoryList = storeList.get(i).get("category").toString().split(",");
//                //JSONObject categoryArray = new JSONObject();
//                String categoryArray = "";
//                for(int j=0;j<categoryList.length; j++){
//                    String category_str = Db.queryStr("select vcode from xj_enum_item where id = ?",categoryList[j]);
//                    categoryArray += category_str+",";
//                    if(j == categoryList.length-1){
//                        categoryArray = categoryArray.substring(0,categoryArray.length()-1);
//                    }
//                }
//                storeList.get(i).set("category",categoryArray);
//            }
        }

        for(int i = 0; i<storeList.size();i++){
            storeList.get(i).set("logo",domain_url+"/store/logo/"+storeList.get(i).get("logo"));
            //产品图
            if(!StringUtils.isEmpty(storeList.get(i).get("product_show_pic"))){
                String[] productList = storeList.get(i).get("product_show_pic").toString().split(",");
                JSONArray productArray = new JSONArray();
                for(int j=0;j<productList.length; j++){
                    JSONObject productOject = new JSONObject();
                    String product_str = domain_url+"/store/product/"+productList[j];
                    productOject.put("url",product_str);
                    productArray.add(productOject);
                }
                storeList.get(i).set("product_show_pic", productArray);
            }

        }

        renderJson(storeList);
    }


    //品类搜索查询
    public void getStoreCategoryBySearch(@Para("search_str") String search_str,@Para("language") String language){
        List<Record> storeList;
        List<Record> storeListArray;
        List<Record> categorySearchList;
        String sql="";


        if(language.equalsIgnoreCase("cn")){

            categorySearchList = Db.find("SELECT\n" +
                    "\tid\n" +
                    "FROM\n" +
                    "\txj_enum_item\n" +
                    "WHERE\n" +
                    "\tenum_value LIKE '%"+search_str+"%'");


            sql = "SELECT DISTINCT t.id,t.market_type,t.category,t.mainlabel,t.store_name,t.store_no,t.logo,t.phone,t.product_show_pic from (";


            for (int a = 0;a<categorySearchList.size();a++){

                  if(a == 0){
                      sql += "SELECT\n" +
                              "\tid,\n" +
                              "\tmarket_type,\n" +
                              "\tcategory,\n" +
                              "\tmainlabel,\n" +
                              "\tstore_name,\n" +
                              "\tstore_no,\n" +
                              "\tlogo,\n" +
                              "\tphone,\n" +
                              "\tproduct_show_pic\n" +
                              "FROM\n" +
                              "\txj_store\n" +
                              "WHERE\n" +
                              "\tcategory LIKE '%"+categorySearchList.get(a).get("id")+"%'";
                  }else{
                      sql += " union ALL ";
                      sql += "SELECT\n" +
                              "\tid,\n" +
                              "\tmarket_type,\n" +
                              "\tcategory,\n" +
                              "\tmainlabel,\n" +
                              "\tstore_name,\n" +
                              "\tstore_no,\n" +
                              "\tlogo,\n" +
                              "\tphone,\n" +
                              "\tproduct_show_pic\n" +
                              "FROM\n" +
                              "\txj_store\n" +
                              "WHERE\n" +
                              "\tcategory LIKE '%"+categorySearchList.get(a).get("id")+"%'";
                  }



            }
            sql += ") t";

            storeList =  Db.find(sql);

            for(int i = 0; i<storeList.size();i++){
                //主营
                String[] categoryList = storeList.get(i).get("category").toString().split(",");
                //JSONObject categoryArray = new JSONObject();
                String categoryArray = "";
                for(int j=0;j<categoryList.length; j++){
                    String category_str = Db.queryStr("select enum_value from xj_enum_item where id = ?",categoryList[j]);
                    categoryArray += category_str+",";
                    if(j == categoryList.length-1){
                        categoryArray = categoryArray.substring(0,categoryArray.length()-1);
                    }
                }
                storeList.get(i).set("category",categoryArray);
            }

        }else{
            categorySearchList = Db.find("SELECT\n" +
                    "\tid\n" +
                    "FROM\n" +
                    "\txj_enum_item\n" +
                    "WHERE\n" +
                    "\tvcode LIKE '%"+search_str+"%'");

            sql = "SELECT DISTINCT t.id,t.market_type,t.category,t.mainlabel_en as mainlabel ,t.store_name_en as store_name,t.store_no,t.logo,t.phone,t.product_show_pic from (";
            for (int a = 0;a<categorySearchList.size();a++){

                if(a == 0){
                    sql += "SELECT\n" +
                            "\tid,\n" +
                            "\tmarket_type,\n" +
                            "\tcategory,\n" +
                            "\tmainlabel_en as mainlabel,\n" +
                            "\tstore_name_en as store_name,\n" +
                            "\tstore_no,\n" +
                            "\tlogo,\n" +
                            "\tphone,\n" +
                            "\tproduct_show_pic\n" +
                            "FROM\n" +
                            "\txj_store\n" +
                            "WHERE\n" +
                            "\tcategory LIKE '%"+categorySearchList.get(a).get("id")+"%'";
                }else{
                    sql += " union ALL ";
                    sql += "SELECT\n" +
                            "\tid,\n" +
                            "\tmarket_type,\n" +
                            "\tcategory,\n" +
                            "\tmainlabel_en as mainlabel,\n" +
                            "\tstore_name_en as store_name,\n" +
                            "\tstore_no,\n" +
                            "\tlogo,\n" +
                            "\tphone,\n" +
                            "\tproduct_show_pic\n" +
                            "FROM\n" +
                            "\txj_store\n" +
                            "WHERE\n" +
                            "\tcategory LIKE '%"+categorySearchList.get(a).get("id")+"%'";
                }



            }
            sql += ") t";


            storeList =  Db.find(sql);

//            for(int i = 0; i<storeList.size();i++){
//                //主营
//                String[] categoryList = storeList.get(i).get("category").toString().split(",");
//                //JSONObject categoryArray = new JSONObject();
//                String categoryArray = "";
//                for(int j=0;j<categoryList.length; j++){
//                    String category_str = Db.queryStr("select vcode from xj_enum_item where id = ?",categoryList[j]);
//                    categoryArray += category_str+",";
//                    if(j == categoryList.length-1){
//                        categoryArray = categoryArray.substring(0,categoryArray.length()-1);
//                    }
//                }
//                storeList.get(i).set("category",categoryArray);
//            }
        }

        for(int i = 0; i<storeList.size();i++){
            storeList.get(i).set("logo",domain_url+"/store/logo/"+storeList.get(i).get("logo"));
            //产品图
            if(!StringUtils.isEmpty( storeList.get(i).get("product_show_pic"))){
                String[] productList = storeList.get(i).get("product_show_pic").toString().split(",");
                JSONArray productArray = new JSONArray();
                for(int j=0;j<productList.length; j++){
                    JSONObject productOject = new JSONObject();
                    String product_str = domain_url+"/store/product/"+productList[j];
                    productOject.put("url",product_str);
                    productArray.add(productOject);
                }
                storeList.get(i).set("product_show_pic", productArray);
            }

        }

         renderJson(storeList);

    }

    /**
     * 获取品牌展示的主营业务列表
     */

    public void getCategoryList(@Para("language") String language){
        List<Record> categoryList;
        if(language.equalsIgnoreCase("cn")){
            categoryList = Db.find("SELECT\n" +
                    "\tid,\n" +
                    "\tenum_value AS category\n" +
                    "FROM\n" +
                    "\txj_enum_item\n" +
                    "WHERE\n" +
                    "\tenum_id = 3");
        }else{
            categoryList = Db.find("SELECT\n" +
                    "\tid,\n" +
                    "\tvcode AS category\n" +
                    "FROM\n" +
                    "\txj_enum_item\n" +
                    "WHERE\n" +
                    "\tenum_id = 3");
        }
        renderJson(categoryList);
    }

    /**
     * 通过id获取主营业务单项名称
     */
    public void getCategoryName(@Para("language") String language,@Para("id") String id){
        String category_name = "";
        if(language.equalsIgnoreCase("cn")){
             category_name = Db.queryStr("SELECT\n" +
                    "\tenum_value AS category\n" +
                    "FROM\n" +
                    "\txj_enum_item\n" +
                    "WHERE\n" +
                    "\tid = ?",id);
        }else{
            category_name = Db.queryStr("SELECT\n" +
                    "\tvcode AS category\n" +
                    "FROM\n" +
                    "\txj_enum_item\n" +
                    "WHERE\n" +
                    "\tid = ?",id);
        }
        renderJson(category_name);
    }

    WxMaService wxService = WxMaConfiguration.getMaService();
    /**
     * 专享二维码链接
     */
    public  void qrCode(@Para("id") String store_id){
        File qrcode = null;
        String wApath =  "pages/shopdetailpage/shopdetailpage" +"?id="+store_id;

        //检查是否已经调用过
        String qrcodeFilePath = Db.queryStr("select wx_qrcode_file from xj_store where id = ?",store_id);
        String path = "E:/xj-project-3.3.0-release/webapp/store/qrcode/";
        if (qrcodeFilePath != null){
            qrcode = new File(path+qrcodeFilePath);
            if (qrcode.exists()) {
//                renderFile(qrcode);
                renderJson(qrcodeFilePath);
                return;
            }
        }
        //没用调用过则调取维修接口获取
        String fileName =  null;
        WxMaQrcodeService qrcodeService = wxService.getQrcodeService();
        try {
            File qrcodetemp = qrcodeService.createQrcode(wApath);
            fileName =  DateUtil.today() + store_id +"." +FileUtil.getType(qrcodetemp);
            qrcode = new File(path+fileName);
            FileUtil.copy(qrcodetemp, qrcode,true);
        } catch (WxErrorException e) {
            e.printStackTrace();

        }

//        Record r  = new Record();
//        r.set("wx_qrcode_file",fileName);
        Db.update("update xj_store set wx_qrcode_file = '"+fileName+"' where id = "+store_id);
        //repairworker.setQrcodeFile(fileName).update();
//        renderFile(qrcode);
        renderJson(fileName);
    }


    /**
     * 市场二维码
     */
    public  void qrMarketCode(@Para("id") String market_id){
        File qrcode = null;
        String wApath =  "pages/market/market" +"?id="+market_id;

        //检查是否已经调用过
        //String qrcodeFilePath = Db.queryStr("select wx_qrcode_file from xj_store where id = ?",market_id);
        String path = "E:/xj-project-3.3.0-release/webapp/store/qrcode/";

        //没用调用过则调取维修接口获取
        String fileName =  null;
        WxMaQrcodeService qrcodeService = wxService.getQrcodeService();
        try {
            File qrcodetemp = qrcodeService.createQrcode(wApath);
            fileName =  DateUtil.today() + market_id +"." +FileUtil.getType(qrcodetemp);
            qrcode = new File(path+fileName);
            FileUtil.copy(qrcodetemp, qrcode,true);
        } catch (WxErrorException e) {
            e.printStackTrace();

        }
        renderJson(fileName);
    }

    public void PostGet(@Para("market_id") String market_id){
      String poster = Db.queryStr("select shop_pic from xj_introduce where id =?",market_id);
      poster = domain_url+"/org/"+poster;
      renderJson(poster);
    }

    public void GetAllSearch(@Para("language") String language){
        List<Record> hot_data;
        if(language.equalsIgnoreCase("cn")){
            hot_data =  Db.find("select DISTINCT enum_value from xj_enum_item where enum_id in (9,10) and enum_value is not null;");
        }else{
            hot_data =  Db.find("select DISTINCT vcode as enum_value from xj_enum_item where enum_id in (9,10) and vcode is not null;");
        }

        renderJson(hot_data);
    }

    public void GetStoreSearch(@Para("language") String language){
        List<Record> hot_data;
        if(language.equalsIgnoreCase("cn")){
            hot_data =  Db.find("select enum_value from xj_enum_item where enum_id = 9 and enum_value is not null;");
        }else{
            hot_data =  Db.find("select vcode as enum_value from xj_enum_item where enum_id = 9 and vcode is not null;");
        }
        renderJson(hot_data);
    }

    public void GetCategorySearch(@Para("language") String language){
        List<Record> hot_data;
        if(language.equalsIgnoreCase("cn")){
            hot_data =  Db.find("select enum_value from xj_enum_item where enum_id = 10 and enum_value is not null;");
        }else{
            hot_data =  Db.find("select vcode as enum_value from xj_enum_item where enum_id = 10 and vcode is not null;");
        }
        renderJson(hot_data);
    }

    public void getVr(@Para("id") int id){
        String vr = Db.queryStr("select qrcode from xj_introduce where id = ?",id);
        renderJson(domain_url+"/org/"+vr);
    }

    public void getVRUrl(@Para("id") int id){
        String vr = Db.queryStr("select VR_url from xj_introduce where id = ?",id);
        renderJson(vr);
    }

    public void  getWpConig() throws WxErrorException {
        WxMpService wxMpService = WxMpConfiguration.getWxMpService();
//        String jsapiTicket = wxMpService.getJsapiTicket();
        WxJsapiSignature jsapiSignature = wxMpService.createJsapiSignature(getPara("clientUrl"));
        renderJson(jsapiSignature);
    }


}
